package View;


import Listening.Enroll;
//import jt.dengluJT;
//import jt.zhuceJT;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.sql.*;


public class Login extends JFrame {
    JFrame jf = new JFrame("学生成绩管理系统");
    JLabel ts1 = new JLabel();
    JTextField uf = new JTextField(15);


    JPasswordField puf = new JPasswordField(15);

    public void zb() {

//        jf.setBounds(400, 300,);
        URL img= Login.class.getClassLoader().getResource("1.png");
        setIconImage(new ImageIcon(img).getImage());
        jf.setResizable(false);
        ImageIcon bg = new ImageIcon("./souse/images/b.jpg");
        JLabel label = new JLabel(bg);
        label.setSize(bg.getIconWidth(), bg.getIconHeight());
        jf.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
        //2.把窗口面板设为内容面板并设为透明、流动布局。
        JPanel pan = (JPanel) jf.getContentPane();
        pan.setOpaque(false);
        pan.setLayout(new FlowLayout());


        Box b = Box.createVerticalBox();
        Box ub = Box.createHorizontalBox();
        JLabel jl = new JLabel("用户名");

        ub.add(jl);
        ub.add(Box.createHorizontalStrut(20));
        ub.add(uf);
        Box pb = Box.createHorizontalBox();
        JLabel pw = new JLabel("密    码");

        pb.add(pw);
        pb.add(Box.createHorizontalStrut(20));
        pb.add(puf);
        Box anniu = Box.createHorizontalBox();
        JButton denglu = new JButton("登录");
        JButton zhuce = new JButton("修改初始密码");
        anniu.add(denglu);
        anniu.add(Box.createHorizontalStrut(100));
        anniu.add(zhuce);
        Box ts = Box.createHorizontalBox();


        ts.add(ts1);

        b.add(Box.createVerticalStrut(40));
        b.add(ub);
        b.add(Box.createVerticalStrut(40));
        b.add(pb);
        b.add(Box.createVerticalStrut(40));
        b.add(anniu);
        b.add(Box.createVerticalStrut(40));
        b.add(ts);
        //3.之后把组件和面板添加到窗口面板；
        jf.setSize(bg.getIconWidth(), bg.getIconHeight());
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(b);
        denglu.addActionListener(new dengluJT());
        zhuce.addActionListener(new zhuceJT());
        jf.setLayout(new FlowLayout());
        jf.setVisible(true);
    }

//    public static void main(String[] args) {
//
//        new DengLu().zb();
//    }

    public class dengluJT implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            ts1.setForeground(Color.red);
            String zhang=uf.getText();
            String mi=new String(puf.getPassword());
//            System.out.println(mi);

        if (uf.getText().equals("")||mi.equals("")){
            ts1.setText("用户名或密码不能为空");
        }else {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                Connection c= DriverManager.getConnection("jdbc:mysql://localhost:3306/lxt","root","111111");
                Statement s=c.createStatement();
                ResultSet result=s.executeQuery("select *from manager where user_name='"+zhang+"'");
//                ResultSet result1=s.executeQuery("select *from evaluate where 学号='"+zhang+"'");
                if (result.next()){
//                    System.out.println(result.getInt("id")+result.getString("user")+result.getString("ps"));
                    ResultSet r=s.executeQuery("select *from manager where pwd='"+mi+"'");
                    if (r.next()){
                        new ZhuJieMian().into();
                        jf.dispose();
                    }else {
                        ts1.setText("密码错误");

                    }
                }else {
                    ts1.setText("该学号不存在或学号输入错误");

                }
                result.close();
                s.close();
                c.close();

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } catch (ClassNotFoundException classNotFoundException) {
                classNotFoundException.printStackTrace();
            }
        }
            if (uf.getText().equals("")||mi.equals("")){
                ts1.setText("用户名或密码不能为空");
            }else {
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    Connection c= DriverManager.getConnection("jdbc:mysql://localhost:3306/lxt","root","111111");
                    Statement s=c.createStatement();
//                    ResultSet result=s.executeQuery("select *from 注册表 where username='"+zhang+"'");
                    ResultSet result1=s.executeQuery("select *from evaluate where 学号='"+zhang+"'");


                    if (result1.next()){
//                    System.out.println(result.getInt("id")+result.getString("user")+result.getString("ps"));
//                        String ss= result1.getString("面向对象");
//                        System.out.println(ss);
                        ResultSet r=s.executeQuery("select *from evaluate where 密码='"+mi+"'");
                        if (r.next()){

                            new StudentLogin(zhang);
//                            if (ss.equals("")){
//                                new xsDengLu(zhang);
                                jf.dispose();
//                            }else {
////                               new xsDengLu(zhang);
//                            }
////
                        }else {
                            ts1.setText("密码错误");

                        }
                    }else {
                        ts1.setText("该学号不存在或学号输入错误");

                    }
                    result1.close();
                    s.close();
                    c.close();

                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                } catch (ClassNotFoundException classNotFoundException) {
                    classNotFoundException.printStackTrace();
                }
            }

        }
    }
    public class zhuceJT implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            new Enroll().into();
            jf.dispose();
        }
    }

    }
